<?php

App::uses('AppController', 'Controller');

/**
 * Groups Controller
 *
 * @property Group $Group
 */
class AnswersController  extends AppController {

    function beforeFilter() {
        $this->layout = "backend"; 
        parent::beforeFilter();        
       
    }    
    /**
     * 
     * index method
     *
     * @return void
     */
    public function index() { 
    	$isGroup =$this->Auth->user('Group');	
		$this->Answer->recursive = 0;
		if($isGroup['name']=="Superusers"){
			$conditions = array();
		}else{
			$conditions = array('Answer.id_user'=>$this->Auth->user('id'));
		}
		
        $this->paginate = array(
		        'conditions'=>$conditions,
		        
	       	 );    	
       
        $this->set('answers', $this->paginate('Answer'));
        $this->render('/Backend/Answers/index');
        
    }

    /**
     * add method
     *
     * @return void
     */
    public function add($id= null) {    
    	$this->loadModel("Question");
    	//$data=$this->Question->findById($id);	
    	$question=$this->Question->read(null, $id);
    	
    	$ans = $this->Answer->find('all',array('conditions'=>array('Answer.id_quyestion'=>$id)));
    	
    	$this->set(array('quyetion'=>$question,
    					 'ans'=>$ans,
    					));
     if ($this->request->is('post')) {
     		
            $this->Answer->create();
            	$this->request->data['Answer']['id_user']=$this->Auth->user('id');
            if ($this->Answer->save($this->request->data)) {
                $this->Session->setFlash(__('The Question has been saved'), 'success');
                //$this->redirect(array('action' => 'add',$id));
                 $this->redirect($this->referer());
            } else {
                $this->Session->setFlash(__('The Question could not be saved. Please, try again.'), 'error');
            }
        }
         $this->render('/Backend/Answers/add');
    }
	/**
	 * active method
	 * @param string $id
	 * @return void
	 */
	public function active($id = null) {
		$this->Question->id = $id;		
		if (!$this->Question->exists()) {
            throw new NotFoundException(__('Invalid question'));
        }
	  if ($this->Question->id) {
	  		  $data= array('Question'=> array('id' =>$this->Question->id,'status' => '1'));
		  if($this->Question->saveall($data)){
	                $this->Session->setFlash(__('The question has been actived'));
	                $this->redirect($this->referer());
	      }else{
	      	 		$this->Session->setFlash(__('The user could not be active. Please, try again.'));
	                $this->redirect($this->referer());
	      }		  		
        }else{
        	$this->redirect($this->referer());
        }
	}
	/**
	 * finish method
	 * @return void
	 */
	public function finish() {
		$datas = $this->Question->find('list',array('conditions'=>array('Question.status'=>1),
													'fields' => array('Question.id'),
													));
		
		if(count($datas)>0){
			foreach($datas as $key=>$data){
				$up[$key]=array('Question'=>array('id'=>$key,'status'=>2));
			}
			if($this->Question->saveall($up)){                
                $this->Session->setFlash(__('The question has been finish'), 'success');
                $this->redirect($this->referer());
             }else{             	
             	 $this->Session->setFlash(__('The question could not be finish. Please, try again.'), 'error');
                $this->redirect($this->referer());
             }
		}else{
			 $this->redirect($this->referer());
		}
	}
    /**
     * edit method
     *
     * @param string $id
     * @return void
     */
    public function edit($id = null) {
    	$this->Answer->id = $id;
   		
        if (!$this->Answer->exists()) {
            throw new NotFoundException(__('Invalid user'));
        }
        
        if ($this->request->is('post') || $this->request->is('put')) {
        	#$this->request->data['Question']['status']=0;
        	#$this->request->data['Question']['id_user']=$_SESSION['Auth']['User']['id'];	
            if ($this->Answer->save($this->request->data)) {
                $this->Session->setFlash(__('The question has been saved'), 'success');
                $this->redirect($this->request->data['Answer']['url']);
                
            } else {
                $this->Session->setFlash(__('The user could not be saved. Please, try again.'), 'error');
            }
        } else {
            $this->request->data = $this->Answer->read(null, $id);            
        }
         $this->render('/Backend/Answers/edit');
    }
    
public function edits($id = null) {
    	$this->Answer->id = $id;
   		
        if (!$this->Answer->exists()) {
            throw new NotFoundException(__('Invalid user'));
        }
        
        if ($this->request->is('post') || $this->request->is('put')) {
        	#$this->request->data['Question']['status']=0;
        	#$this->request->data['Question']['id_user']=$_SESSION['Auth']['User']['id'];	
            if ($this->Answer->save($this->request->data)) {
                $this->Session->setFlash(__('The question has been saved'), 'success');
               $this->redirect(array('action' => 'index'));
                
            } else {
                $this->Session->setFlash(__('The user could not be saved. Please, try again.'), 'error');
            }
        } else {
            $this->request->data = $this->Answer->read(null, $id);            
        }
    }

    /**
     * delete method
     *
     * @param string $id
     * @return void
     */
    public function delete($id = null) {
        if (!$this->request->is('post')) {
            throw new MethodNotAllowedException();
        }
        $this->Answer->id = $id;
        if (!$this->Answer->exists()) {
            throw new NotFoundException(__('Invalid group'), 'error');
        }
        if ($this->Answer->delete()) {
            $this->Session->setFlash(__('Question deleted'), 'success');
           // $this->redirect(array('action' => 'index'));
           $this->redirect($this->referer());
        }
        $this->Session->setFlash(__('Question was not deleted'), 'error');
        //$this->redirect(array('action' => 'index'));
        $this->redirect($this->referer());
    }
    

}
?>